package com.zn.service.impl;

import com.zn.asycn.UploadWatermark;
import com.zn.entity.AppAgreement;
import com.zn.entity.AppCustomer;
import com.zn.entity.CustomerAgreement;
import com.zn.mapper.AppAgreementMapper;
import com.zn.mapper.AppCustomerMapper;
import com.zn.mapper.CustomerAgreementMapper;
import com.zn.service.AppAgreementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YinJiaWei
 * @version 2019/1/10 10:46
 */
@Service
public class AppAgreementServiceImpl implements AppAgreementService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppAgreementServiceImpl.class);
    @Autowired
    private AppAgreementMapper appAgreementMapper;
    @Autowired
    private CustomerAgreementMapper customerAgreementMapper;
    @Autowired
    private UploadWatermark uploadWatermark;
    @Autowired
    private AppCustomerMapper appCustomerMapper;

    @Override
    public AppAgreement getAgreementByAgreementCode(String agreementCode) {
        AppAgreement appAgreement=new AppAgreement();
        appAgreement.setAgreementCode(agreementCode);
        List<AppAgreement> appAgreements=appAgreementMapper.selectBySelective(appAgreement);
        return appAgreements==null?null:appAgreements.get(0);
    }

    @Override
    public AppAgreement getRiskTipAgreement(String agreementCode, String customerId) {
        AppAgreement appAgreementWhere=new AppAgreement();
        appAgreementWhere.setAgreementCode(agreementCode);
        List<AppAgreement> appAgreements=appAgreementMapper.selectBySelective(appAgreementWhere);
        if(appAgreements!=null && !appAgreements.isEmpty()){
            AppAgreement appAgreement=appAgreements.get(0);
            CustomerAgreement customerAgreementWhere=new CustomerAgreement();
            customerAgreementWhere.setAgreementId(appAgreement.getAgreementId());
            customerAgreementWhere.setCustomerId(customerId);
            customerAgreementWhere.setEffectiveStatus(0);
            List<CustomerAgreement> customerAgreements=customerAgreementMapper.selectBySelective(customerAgreementWhere);
            if(customerAgreements==null || customerAgreements.size()==0){
                /**如果第一次 没有授权则返回风险提示协议 如果已授权知晓则返回空**/
                return appAgreement;
            }
        }
        return null;
    }

    @Override
    public boolean customerKnownAgreement(String agreementId, String customerId) {
        CustomerAgreement customerAgreementWhere=new CustomerAgreement();
        customerAgreementWhere.setAgreementId(agreementId);
        customerAgreementWhere.setCustomerId(customerId);
        customerAgreementWhere.setEffectiveStatus(0);
        List<CustomerAgreement> customerAgreements=customerAgreementMapper.selectBySelective(customerAgreementWhere);
        LOGGER.info("111customerAgreements.size------>"+customerAgreements.size());
        /**校验客户是否已知晓 已经知晓返回true 未知晓返回false**/
        if(customerAgreements!=null && !customerAgreements.isEmpty()){
            return true;
        }
        LOGGER.info("2222customerAgreements.size------>"+customerAgreements.size());
        AppCustomer appCustomer=appCustomerMapper.selectByPrimaryKey(customerId);
        AppAgreement appAgreementWhere=new AppAgreement();
        appAgreementWhere.setAgreementId(agreementId);
        List<AppAgreement> appAgreements=appAgreementMapper.selectBySelective(appAgreementWhere);
        if(appCustomer!=null && !appAgreements.isEmpty()){
            uploadWatermark.customerKnownAndCreateWatermark(appAgreements.get(0),appCustomer.getCustomerId(),appCustomer.getCustomerTradeAccount());
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<AppAgreement> getAgreementList(String customerId) {
        AppAgreement appAgreementWhere=new AppAgreement();
        appAgreementWhere.setAgreementStatus(0);
        List<AppAgreement> appAgreements=appAgreementMapper.selectBySelective(appAgreementWhere);
        for(AppAgreement appAgreement:appAgreements){
            CustomerAgreement customerAgreementWhere=new CustomerAgreement();
            customerAgreementWhere.setAgreementId(appAgreement.getAgreementId());
            customerAgreementWhere.setCustomerId(customerId);
            customerAgreementWhere.setEffectiveStatus(0);
            List<CustomerAgreement> customerAgreements=customerAgreementMapper.selectBySelective(customerAgreementWhere);
            if(customerAgreements!=null && !customerAgreements.isEmpty()){
                appAgreement.setAgreementPic(customerAgreements.get(0).getCustomerAgreementPic());
            }
        }
        return appAgreements;
    }
}
